
package com.aris.voicechecking;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicecheck.adapter.WeekLessonAdapter;
import com.voicecheck.adapter.WeekLessonAdapter.OnItemClickListener;
import com.voicecheck.model.WeekLesson;
import com.voicecheck.provider.DataProviderV2;
import com.voicecheck.support.DividerItemDecoration;

public class WeekLessonActivity extends ActionBarActivity {

    protected RecyclerView listLesson;

    protected WeekLessonAdapter lessonAdapter;

    protected List<WeekLesson> listLessonTitle;

    private RelativeLayout adView;

    protected DataProviderV2 dataProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_week_lesson);

        initial();
        execution();

    }

    @Override
    protected void onResume() {

        super.onResume();
    }

    private void initial() {
        listLesson = (RecyclerView)findViewById(R.id.list_lesson);
        listLessonTitle = new ArrayList<WeekLesson>();
        lessonAdapter = new WeekLessonAdapter(listLessonTitle);
        adView = (RelativeLayout)findViewById(R.id.adView);
        dataProvider = new DataProviderV2(this);
    }

    private void execution() {

        adView.setBackgroundResource(R.drawable.banner);

        adView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent webviewIntent = new Intent(WeekLessonActivity.this, WebviewActivity.class);
                startActivity(webviewIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);

            }
        });

        int numOfWeed = dataProvider.getWeekNumber();

        for (int i = 1; i <= numOfWeed; i++) {
            WeekLesson wlesson = new WeekLesson();
            wlesson.setTitle("Week" + i + ": Day1 ~ Day4 (Q6x4)");
            listLessonTitle.add(wlesson);
        }

        listLesson.setHasFixedSize(true);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        listLesson.setLayoutManager(mLayoutManager);

        listLesson.setAdapter(lessonAdapter);

        listLesson.addItemDecoration(new DividerItemDecoration(this, null));
        listLesson.setItemAnimator(new DefaultItemAnimator());

        lessonAdapter.SetOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {

                Intent dayintent = new Intent(WeekLessonActivity.this, DayLessonActivity.class);

                dayintent.putExtra("weeknumber", position + 1);

                startActivity(dayintent);

                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);
            }
        });

        // configuration actionbar
        // getSupportActionBar().setTitle(getString(R.string.back));
        ActionBar actionBar = getSupportActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(getResources().getColor(R.color.main_color));
        actionBar.setBackgroundDrawable(colorDrawable);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
        View actionbarcustomeview = getLayoutInflater().inflate(R.layout.actionbar_center_title,
                (ViewGroup)(getSupportActionBar().getCustomView()));

        TextView center_title = (TextView)actionbarcustomeview.findViewById(R.id.center_title);
        center_title.setText(getString(R.string.lesson));
        center_title.setTextColor(Color.WHITE);

        actionBar.setCustomView(actionbarcustomeview, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lesson, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        overridePendingTransition(R.drawable.anim_slide_in_right, R.drawable.anim_slide_out_right);
    }
}
